package com.box.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.annotation.SysLog;
import com.box.annotation.SystemLogAspect;
import com.box.entity.DbLog;
import com.box.entity.DbOther;
import com.box.entity.DbUsers;
import com.box.entity.DbWeb;
import com.box.service.LogService;
import com.box.service.OtherService;
import com.box.service.UserService;
import com.box.service.WebService;
import com.box.utils.CookieUtils;
import com.box.utils.LayuiResult;
import com.box.utils.ResponseResult;
import com.box.utils.SessionUtils;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/member")
public class MemberController {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private WebService webService;
	
	@Autowired
	private LogService logService;
	
	@Autowired
	private OtherService otherService;
	
	@SysLog("会员信息修改，会员自己修改")
	@RequestMapping(value="/update/userinfo",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ResponseResult updateUserInfo(String userName,String passWord,String telphone,String nickname) throws Exception{
		
	passWord=	DigestUtils.md5DigestAsHex(passWord.getBytes());

	return 	userService.updateUser(userName, nickname, passWord, Long.parseLong(telphone));
		
	}
	
	/**
	 * 
	 * 用户登录
	 * @param userName
	 * @param passWord
	 * @return
	 */
	@SysLog("会员登录")
	@RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult MmberLogin(String userName,String passWord,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		passWord=DigestUtils.md5DigestAsHex(passWord.getBytes());
	
		return 		userService.selUsersByUserNameAndPassword(userName, passWord);
	}
	/**
	 * 
	 * 用户注册
	 * @param userName
	 * @param passWord
	 * @return
	 */
	@SysLog("会员注册")
	@RequestMapping(value="/register",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult MmberRegister(String userName,String passWord,String phone,String email) throws Exception{
		System.out.println(userName+".....前端传来的用户名");
		System.out.println(passWord+".....前端传来的密码");
		System.out.println(phone+".....前端传来的手机号");
		System.out.println(email+".....前端传来的邮箱");

		return 		userService.insertUsersBySelectTive(userName, passWord, phone, email);
	}
	
	
	@SysLog("会员信息修改")
	@RequestMapping(value="/UpdateMmber",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult UpdateMmber(DbUsers users) throws Exception{
		
		Integer n = userService.updateByUsers(users);
		if(n>0){
			return new LayuiResult(n, "用户信息修改成功");
		}else{
			return new LayuiResult(500, "用户信息修改失败");
		}

	}
	
	
	
	/**
	 * 
	 * 页面跳转逻辑
	 * 
	 * @return
	 */
	@RequestMapping(value="/page/login",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberLogin(HttpServletRequest request,Model model){
		
		
		
			
	
		
				return "member/login";
	
	
	}
	/**
	 * 
	 * 
	 * 
	 * 	DbWeb web = webService.selWebByOne(4);
		model.addAttribute("members", web);
	
				try {
				
					DbUsers users =	 userService.selUsersByUserName();
				model.addAttribute("user", users);
				String count = SessionUtils.getSessionAttribute("count").toString();
				Integer cont=Integer.parseInt(count);
				Integer sum=++cont;
				model.addAttribute("count", sum);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println("获取用户名失败，页面集中到一起，不登录无法获取到用户名");
		
				}
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/page/register",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberRegister(Model model){
		

			
	
	
	
				return "member/register";
	
	
	}
	
	
	@RequestMapping(value="/page/main",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberMain(Model model){
		

		
	
		try {
			
			
			DbUsers users =	 userService.selUsersByUserName();
	
				model.addAttribute("user", users);
				
				DbWeb web = webService.selWebByOne(4);
				model.addAttribute("members", web);
				
				List<DbLog> log = logService.getLogList();
				
				model.addAttribute("log", log);
				
			
				
				
			return "member/main";
	
		} catch (Exception e) {
			// TODO: handle exception
			return "error/404";
		}
				
				
					
					
					
					
					
					
			
			
		
	
	
	
		
	
	
	}
	
	
	@RequestMapping(value="/page/report",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberReport(Model model){
		


			
			
			try {
				DbUsers users =	 userService.selUsersByUserName();
				model.addAttribute("user",users);		
	DbOther time = otherService.selRegister_TimeByUid(users.getUid().intValue());
				
				model.addAttribute("o",time);
				return "member/report";
			} catch (Exception e) {
				// TODO: handle exception
				return "error/404";
			}
	


	
	
		
	
	
	}
	
	
	@RequestMapping(value="/page/memberinfo",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberMemberinfo(Model model){
		
		try {
			DbUsers users =	 userService.selUsersByUserName();

			model.addAttribute("user", users);
			
			return "member/memberinfo";
		} catch (Exception e) {
			// TODO: handle exception
			String error="您的登录时间已过期请重新登录";
		model.addAttribute("error", error);
	
		return "error/404";
			
		}

	
	
	}
	
	@RequestMapping(value="/page/buycopun",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberBuycopun(Model model){
		

		try {
			DbUsers users =	 userService.selUsersByUserName();
			model.addAttribute("user", users);
		
			return "member/buycopun";
		} catch (Exception e) {
			// TODO: handle exception

			return "error/404";
		}

		


	
	
	}
	
	
	@RequestMapping(value="/page/buyCredtis",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberbuyCredtis(Model model){
		

		try {
			DbUsers users =	 userService.selUsersByUserName();
			model.addAttribute("user",users);
			
			return "member/buyCredtis";
		} catch (Exception e) {
			// TODO: handle exception
			return "error/404";
		}
		
	
	
	}
	
	
	@RequestMapping(value="/page/loginlog",method={RequestMethod.POST,RequestMethod.GET})
	public String showMmberloginlog(Model model){
		
		DbUsers users =	 userService.selUsersByUserName();
		if(users!=null){
			
			model.addAttribute("user", users);
		
			return "member/loginlog";
		}else{
			return "error/404";
		}
	
	
	}
	
	@RequestMapping(value="/page/logout",method={RequestMethod.POST,RequestMethod.GET})
	public String logout(Model model){
		Integer n=0;
		try {
			SessionUtils.removeSessionAttribute("MEMBERUSERNAME");
			n=1;
			System.out.println(n+"..............退出成功");
			return "member/login";
		} catch (Exception e) {
			// TODO: handle exception
			n=0;
			System.out.println(n+"..................退出失败");
			return "error/404";
		}
	
	}
	
	
	@RequestMapping(value="/page/QueryOrder",method={RequestMethod.POST,RequestMethod.GET})
	public String goOrderPage(Model model){
	return "Wx/QueryOrder";
	
	}
	
	@RequestMapping(value="/page/WeixinPay",method={RequestMethod.POST,RequestMethod.GET})
	public String wxpay(Model model){
		
		
		try {
			DbUsers users =	 userService.selUsersByUserName();
			model.addAttribute("user",users);
			
			return "member/WeixinPay";
		} catch (Exception e) {
			// TODO: handle exception
			return "error/404";
		}

	
	}
}
